<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF检测测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-result {
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
        }
        .success { background-color: #d4edda; color: #155724; }
        .error { background-color: #f8d7da; color: #721c24; }
        .info { background-color: #d1ecf1; color: #0c5460; }
        .book-list {
            margin-top: 20px;
        }
        .book-item {
            border: 1px solid #ddd;
            margin: 10px 0;
            padding: 15px;
            border-radius: 5px;
        }
        .book-title {
            font-weight: bold;
            font-size: 18px;
            margin-bottom: 5px;
        }
        .book-details {
            color: #666;
            font-size: 14px;
        }
        .book-status {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 3px;
            font-size: 12px;
            margin-left: 10px;
        }
        .status-new {
            background-color: #28a745;
            color: white;
        }
        .status-available {
            background-color: #6c757d;
            color: white;
        }
    </style>
</head>
<body>
    <h1>PDF检测测试</h1>
    <button onclick="testPDFDetection()">开始测试</button>
    <div id="results"></div>
    <div id="bookList" class="book-list"></div>

    <script>
        async function testPDFDetection() {
            const resultsDiv = document.getElementById('results');
            const bookListDiv = document.getElementById('bookList');
            
            resultsDiv.innerHTML = '<div class="test-result info">开始检测PDF文件...</div>';
            bookListDiv.innerHTML = '';

            try {
                // 使用修改后的检测逻辑
                const files = await detectPDFFiles();
                resultsDiv.innerHTML += `<div class="test-result success">发现 ${files.length} 个PDF文件</div>`;
                
                // 生成书籍信息
                const books = [];
                for (const filename of files) {
                    const bookInfo = generateBookInfoFromFilename(filename);
                    books.push(bookInfo);
                }

                // 显示书籍列表
                displayBooks(books);

            } catch (error) {
                resultsDiv.innerHTML += `<div class="test-result error">检测失败: ${error.message}</div>`;
            }
        }

        async function detectPDFFiles() {
            const existingFiles = [];
            
            for (let i = 1; i <= 20; i++) {
                const filename = `jinyouth-issue${i}.pdf`;
                try {
                    const response = await fetch(`pdfs/${filename}`, { method: 'HEAD' });
                    if (response.ok) {
                        existingFiles.push(filename);
                        console.log(`发现PDF文件: ${filename}`);
                    }
                } catch (error) {
                    // 文件不存在，忽略
                }
            }

            console.log(`总共发现 ${existingFiles.length} 个PDF文件`);
            return existingFiles;
        }

        function generateBookInfoFromFilename(filename) {
            const nameWithoutExt = filename.replace(/\.pdf$/i, '');
            const id = nameWithoutExt;

            let title;
            if (nameWithoutExt.includes('《') && nameWithoutExt.includes('》')) {
                title = nameWithoutExt;
            } else if (nameWithoutExt.match(/jinyouth-issue(\d+)/i)) {
                const issueNumber = nameWithoutExt.match(/jinyouth-issue(\d+)/i)[1];
                title = `《缙青年》第${issueNumber}期`;
            } else {
                title = nameWithoutExt;
            }

            const description = `${title} - 缙云青年的思想交流平台，展现青年风采，传递青春正能量。`;

            let status = 'available';
            if (nameWithoutExt.match(/jinyouth-issue(\d+)/i)) {
                const issueNumber = parseInt(nameWithoutExt.match(/jinyouth-issue(\d+)/i)[1]);
                if (issueNumber >= 4) {
                    status = 'new';
                }
            }

            return {
                id: id,
                title: title,
                description: description,
                filename: filename,
                status: status
            };
        }

        function displayBooks(books) {
            const bookListDiv = document.getElementById('bookList');
            
            if (books.length === 0) {
                bookListDiv.innerHTML = '<div class="test-result info">没有发现任何期刊</div>';
                return;
            }

            let html = '<h2>发现的期刊列表：</h2>';
            books.forEach(book => {
                const statusText = book.status === 'new' ? '最新' : '可用';
                const statusClass = `status-${book.status}`;
                
                html += `
                    <div class="book-item">
                        <div class="book-title">
                            ${book.title}
                            <span class="book-status ${statusClass}">${statusText}</span>
                        </div>
                        <div class="book-details">
                            <strong>ID:</strong> ${book.id}<br>
                            <strong>文件名:</strong> ${book.filename}<br>
                            <strong>描述:</strong> ${book.description}
                        </div>
                    </div>
                `;
            });

            bookListDiv.innerHTML = html;
        }
    </script>
</body>
</html>
